package com.mouse.view;

import com.mouse.commen.Message;
import com.mouse.commen.MessageType;
import com.mouse.dao.UserDao;
import com.mouse.po.User;

import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerClient extends JFrame {

    public static void main(String[] args) {
        ServerClient serverClient = new ServerClient();

        serverClient.createJFrame();}


    UserDao userDao = new UserDao();



    public void createJFrame() {
        JLabel jLabel = new JLabel("服务端已启动，监听8881端口，等待客户连接", JLabel.CENTER);
        this.add(jLabel, BorderLayout.CENTER);

        this.setTitle("QQ服务器");
        this.setBounds(505, 305, 350, 250);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            this.server();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void server() throws Exception {

        ServerSocket serverSocket = new ServerSocket(8881);

        while (true) {

            Socket socket = serverSocket.accept();

            InputStream inputStream = socket.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            Message requestMessage = (Message) objectInputStream.readObject();

            switch(requestMessage.getMessageType()){
                case MessageType.LOGIN :{
                    User user = requestMessage.getUser();
                    Message message = new Message();
                    if (userDao.login(user.getUsername(), user.getPwd())!=null) {
                        message.setMessageType(MessageType.LOGIN_SUCCESS);
                    } else {
                        message.setMessageType(MessageType.LOGIN_FAIT);
                    }


                    OutputStream outputStream = socket.getOutputStream();
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(message);
                    break;
                }
                case MessageType.REGISTER:{
                    //向客户端应答的message先new出来
                    Message responseMessage = new Message();
                    //通过客户端的请求message获取到注册的user信息
                    User user = requestMessage.getUser();

                    //通过getByUsername看是否Username已经被注册了没有
                    if(userDao.getByUsername(user.getUsername())==null){
                        //将user信息存入数据库
                        userDao.insertUser(user);
                        responseMessage.setMessageType(MessageType.REGISTER_SUCCESS);
                        responseMessage.setContent("注册成功");
                    }else{
                        responseMessage.setMessageType(MessageType.REGISTER_FAIT);
                        responseMessage.setContent("注册失败，账号已经被注册");
                    }
                    //向客户端输出验证结果
                    OutputStream outputStream = socket.getOutputStream();
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(responseMessage);
                    break;
                }
            }



        }

    }
    }

